library pkg_banner_view;

import 'dart:async';
import 'dart:io';

import 'package:flutter/material.dart';

/// 轮播图
class BannerView extends StatefulWidget {
  ///图片地址
  final List<String> imagePaths;

  ///轮播图高度
  final double height;

  ///横向的内边距
  final double hPadding;

  ///图片圆角
  final double borderRadius;

  BannerView({
    required this.imagePaths,
    this.height = 200,
    this.hPadding = 10,
    this.borderRadius = 5,
  });

  @override
  _BannerViewState createState() => _BannerViewState();
}

class _BannerViewState extends State<BannerView> {
  late final PageController _controller;
  late final Timer _timer;
  int index = 0;

  @override
  void initState() {
    super.initState();
    _controller = PageController();
    _timer = Timer.periodic(Duration(seconds: 3), (time) {
      index++;
      if (index >= widget.imagePaths.length) {
        index = index % widget.imagePaths.length;
      }
      _controller.animateToPage(
        index,
        duration: Duration(milliseconds: 500),
        curve: Curves.linear,
      );
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    _timer.cancel();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> indicator = [];
    for (int i = 0; i < widget.imagePaths.length; i++) {
      if (i == index) {
        indicator.add(
          Container(
            margin: EdgeInsets.only(top: 10, bottom: 10, left: 3, right: 3),
            width: 8,
            height: 8,
            decoration: BoxDecoration(
              color: Colors.green,
              borderRadius: BorderRadius.circular(45),
            ),
          ),
        );
      } else {
        indicator.add(
          Container(
            margin: EdgeInsets.only(top: 10, bottom: 10, left: 3, right: 3),
            width: 8,
            height: 8,
            decoration: BoxDecoration(
              color: Colors.black38,
              borderRadius: BorderRadius.circular(45),
            ),
          ),
        );
      }
    }
    return Stack(
      alignment: Alignment.bottomCenter,
      children: [
        SizedBox(
          height: widget.height,
          width: MediaQuery.of(context).size.width,
          child: PageView(
            physics: BouncingScrollPhysics(),
            onPageChanged: (index) => setState(() {
              this.index = index;
            }),
            controller: _controller,
            children: widget.imagePaths
                .map(
                  (e) => Padding(
                    padding: EdgeInsets.only(
                      left: widget.hPadding,
                      right: widget.hPadding,
                    ),
                    child: ClipRRect(
                      borderRadius: BorderRadius.circular(widget.borderRadius),
                      child: e.contains("http")
                          ? Image.network(
                              e,
                              fit: BoxFit.cover,
                              height: widget.height,
                            )
                          : Image.file(
                              File(e),
                              fit: BoxFit.cover,
                              height: widget.height,
                            ),
                    ),
                  ),
                )
                .toList(),
          ),
        ),
        Row(
          children: indicator,
          mainAxisAlignment: MainAxisAlignment.center,
        )
      ],
    );
  }
}
